<?php

namespace app\sisome\controller;

use think\Paginator;
use think\Request;
use think\Db;

class Users extends Common
{ //留控
    public function index()
    {
        $this->redirect('index/index');
    }

    /**
     * @name 用户列表
     * @author zay
     * @Date 2020/3/19 0019 17:45
     * @return \think\response\View
     */
    public function user_list()
    { //会员列表
        $where = [];
        $orderby = 'regtime';
        $ordersc = 'desc';
        $param = request()->param();
        // dump($param);
        if (!empty($param)) {
            //剔除token
            if (!empty($param['__token__'])) {
                unset($param['__token__']);
            }

            if (!empty($param['page'])) {
                //是翻页 从cookie读取配置（解决翻页后配置项丢失）
                $param = cookie('user_list_param');
            } else {
                //不是翻页 保存筛选配置到cookie
                cookie('user_list_param', $param);
            }

            if ($param) { //解决不筛选 但翻页时 不执行筛选 此时$param=NULL

                //指定用户
                if (!empty($param['uname'])) {
                    $where = ['uname' => $param['uname']];
                } elseif (!empty($param['id'])) {
                    $where = ['id' => $param['id']];
                } else {
                    //排序
                    if (isset($param['orderby']) && isset($param['ordersc'])) {
                        $orderby = $param['orderby'];
                        $ordersc = $param['ordersc'];
                    }
                    //筛选
                    if (isset($param['tname'])) {
                        $where['tname'] = $param['tname'];
                    }
                    if (isset($param['tid'])) {
                        $where['tid'] = $param['tid'];
                    }
                    if (isset($param['regip'])) {
                        $where['regip'] = $param['regip'];
                    }
                }
            }
        } else {
            //无筛选 无翻页 清空配置cookie
            cookie('user_list_param', null);
        }
        // dump($param);
        // dump($where);
        $lists = db('user')->where($where)->order([$orderby => $ordersc])->paginate(10,false, ['query' => Request::instance()->param()]);
        foreach ($lists as $k=>$v){
            $data = $v;
            $data['tj_num'] = Db('user')->where(['tid'=>$v['id']])->count();
            $lists->offsetSet($k,$data);
        }
        $this->assign('param', $param);
        $this->assign('lists', $lists);
        return view();
    }

    public function user_del_fenxi()
    { //会员删除-分析
        $uid = request()->param()['id'];
        $userinfo = userinfobyid($uid);
        if (!$userinfo) {
            die('<p style="color:red;">没有此用户</p>');
        }
        $sanxia = db('user')->where("FIND_IN_SET({$uid},tpath)")->field('uname')->select();

        echo('<p style="color:red;">警告！如非必要，请勿操作此功能，以免造成未知错误！</p>');
        echo('<p style="color:red;">警告！如私钥内有资产，清理后将无法恢复！</p>');
        if (!$sanxia) {
            echo('<p>该用户伞下无人</p>');
        } else {
            echo('<p style="color:red;">警告！该用户伞下拥有会员' . count($sanxia) . '人，将一起被删除！</p>');
            echo('<hr>');
            $i = 1;
            foreach ($sanxia as $sx) {
                echo($i . ' >> ' . $sx['uname']);
                echo('<br>');
                $i++;
            }
        }
    }

    /**
     * @name 删除用户，清空主要的表
     * @author zay
     * @Date 2020/4/2 0002 14:01
     */

    public function user_del_do()
    {   //会员删除-执行
        Db::startTrans();
        try {
            $uid = request()->param()['id'];
            $userinfo = userinfobyid($uid);
            if (!$userinfo) {
                throw new \Exception('没有此用户');
            }
            $list = db('user')->where(['tid' => $uid])->select();
            if ($list) {
                throw new \Exception('伞下有用户，不能删除');
            }
            //删除实名认证
            $shiming = Db('shiming')->where(['uid' => $uid])->find();
            if ($shiming) {
                $res = Db('shiming')->where(['uid' => $uid])->delete();
                if (!$res) {
                    throw new \Exception('删除实名信息失败');
                }
            }
            //清理工单信息
            $gongdan = db('gongdan')->where('uid', $uid)->find();
            if ($gongdan) {
                $ret_gongdan = db('gongdan')->where('uid', $uid)->delete();
                if (!$ret_gongdan) {
                    throw new \Exception('工单信息 清理失败');
                }
            }
            //清理idc流水
            $logome = db('logocoin')->where('uid', $uid)->find();
            if ($logome) {
                $ret_logome = db('logocoin')->where('uid', $uid)->delete();
                if (!$ret_logome) {
                    throw new \Exception('idc流水清理失败');
                }
            }
            //清理矿机
            $kj = db('shop_orderform')->where('uid', $uid)->find();
            if ($kj) {
                $ret_kj = db('shop_orderform')->where('uid', $uid)->delete();
                if (!$ret_kj) {
                    throw new \Exception('矿机 清理成功');
                }
            }
            //删除推荐奖励
            $tuijian = db('invite_reward_coin')->where(['from_uid' => $uid])->find();
            if ($tuijian) {
                $ret_tj = db('invite_reward_coin')->where('from_uid', $uid)->delete();
                if (!$ret_tj) {
                    throw new \Exception('推荐奖励币清理成功');
                }
            }
            //推荐人直推人数减1
           /* $tuser_update = Db('user')->where(['id' => $userinfo['tid']])->setDec('tj_num', 1);
            if(!$tuser_update){
                throw new \Exception('推荐人直推人数更新成功');
            }*/
            $delete_user = DB('user')->where(['id' => $uid])->delete();
            if (!$delete_user) {
                throw new \Exception('删除用户失败');
            }
            db::commit();
            echo('<p style="color:green;">删除成功</p>');
        } catch (\Exception $e) {
            db::rollback();
            echo('<p style="color:red;">' . $e->getMessage() . '</p>');
        }

    }

    public function user_resetpwd()
    {    //重置会员密码
        $post = request()->param();
        if (!isset($post['uname']) || !isset($post['pwd']) || !isset($post['secpwd'])) {
            return json(['zt' => 0, 'msg' => '请完整输入信息']);
        }
        $userinfo = userinfobyuname($post['uname']);
        if (!$userinfo) {
            return json(['zt' => 0, 'msg' => '用户不存在']);
        }
        $ret = db('user')->where('id', $userinfo['id'])->update([
            'pwd' => md5($post['pwd']),
            'secpwd' => md5($post['secpwd'])
        ]);
        if ($ret) {
            return json(['zt' => 1, 'msg' => '修改成功']);
        } else {
            return json(['zt' => 0, 'msg' => '修改失败']);
        }
    }

    public function user_cz()
    { //会员充值
        $request = Request::instance();
        if ($request->isPost()) {
            //充值处理
            $post = $request->param();
            $post['uname'] = trim($post['uname']);
            $post['ome'] = intval($post['ome']);
            $post['kuang'] = intval($post['kuang']);
            $post['quan'] = intval($post['quan']);
            $post['note'] = trim($post['note']) ?: '系统充值';
            $userinfo = userinfobyuname($post['uname']);
            if (!$userinfo) {
                $this->error('无此用户');
            }

            Db::startTrans();   // 启动事务-------------
            $isok = true;
            if ($post['ome'] && $userinfo['ome'] + $post['ome'] >= 0) {
                $ret_ome = db('user')->where('id', $userinfo['id'])->setInc('ome', $post['ome']);
                if (!$ret_ome) $isok = false;
                if ($post['note'] <> 'hkgj') {
                    $log_ome = mylog('ome', $post['uname'], 'sis', '', $userinfo['ome'], $post['ome'], $post['note'], '', '');
                    if (!$log_ome) $isok = false;
                }
            }
            if ($post['kuang'] && $userinfo['kuang'] + $post['kuang'] >= 0) {
                $ret_kuang = db('user')->where('id', $userinfo['id'])->setInc('kuang', $post['kuang']);
                if (!$ret_kuang) $isok = false;
                if ($post['note'] <> 'hkgj') {
                    $log_kuang = mylog('kuang', $post['uname'], 'sis', '', $userinfo['kuang'], $post['kuang'], $post['note'], '', '');
                    if (!$log_kuang) $isok = false;
                }
            }
            if ($post['quan'] && $userinfo['quan'] + $post['quan'] >= 0) {
                $ret_quan = db('user')->where('id', $userinfo['id'])->setInc('quan', $post['quan']);
                if (!$ret_quan) $isok = false;
                if ($post['note'] <> 'hkgj') {
                    $log_quan = mylog('quan', $post['uname'], 'sis', '', $userinfo['quan'], $post['quan'], $post['note'], '', '');
                    if (!$log_quan) $isok = false;
                }
            }

            if ($isok) {
                Db::commit();       // 提交事务-------------
                $this->success($post['uname'] . '充值成功：' . $post['ome'] . ' OME，' . $post['kuang'] . ' 矿金，' . $post['quan'] . ' M币');
            } else {
                Db::rollback();     // 回滚事务-------------
                $this->error('充值失败');
            }
        } else {
            //显示充值记录
            if (isset($request->param()['uname'])) {
                $this->assign('uname', $request->param()['uname']);
            } else {
                $this->assign('uname', '');
            }
            $where['type'] = 'sis';
            $lists_ome = db('logome')->where($where)->order('time DESC')->paginate(10);
            $lists_kuang = db('logkuang')->where($where)->order('time DESC')->paginate(10);
            $lists_quan = db('logquan')->where($where)->order('time DESC')->paginate(10);
            $this->assign('lists_ome', $lists_ome);
            $this->assign('lists_kuang', $lists_kuang);
            $this->assign('lists_quan', $lists_quan);
            return view();
        }
    }

    public function ajax_userinfo()
    {    //ajax请求用户信息
        if (isset(request()->param()['uname'])) {
            $userinfo = userinfobyuname(request()->param()['uname']);
        } elseif (isset(request()->param()['uid'])) {
            $userinfo = userinfobyid(request()->param()['uid']);
        } else {
            return json(['zt' => 0, 'msg' => '请求参数错误：uname、uid']);
        }
        if ($userinfo) {
            return json(['zt' => 1, 'userinfo' => $userinfo]);
        } else {
            return json(['zt' => 0, 'msg' => '无此用户']);
        }
    }

    public function gongdan_list()
    {  //工单列表
        $request = Request::instance();
        $where = [];
        if (isset($request->param()['zt'])) {
            if ($request->param()['zt'] == '1') {
                $where['zt'] = 1;
            } elseif ($request->param()['zt'] == '0') {
                $where['zt'] = 0;
            }
        } else {
            $where['zt'] = 0;
        }
        $where['type'] = 'u';
        $where['qid'] = 0;
        $lists = db('gongdan')->where($where)->order('id asc')->field('id,uid,uname,title,msg,time,zt')->paginate(10);
        $this->assign('lists', $lists);
        return view();
    }

    public function gongdan_zt()
    {   //快速修改工单状态
        $request = Request::instance();
        $zt = $request->param()['zt'];
        if (!in_array($zt, [0, 1])) {
            $this->error('非法操作');
        }
        $ret = db('gongdan')->where('id', $request->param()['id'])->update(['zt' => $request->param()['zt']]);
        if ($ret) {
            $this->success('修改成功');
        } else {
            $this->error('修改失败');
        }
    }

    public function gongdan_info()
    {   //某个工单的回复详情
        $request = Request::instance();
        $qid = $request->param()['id'];

        if (!$request->isPost()) {
            $zt = db('gongdan')->where('id', $qid)->value('zt');
            $lists = db('gongdan')->where('id|qid', $qid)->order('time asc')->select();
            foreach ($lists as &$list) {
                if ($list['imgs']) {
                    $list['imgs'] = json_decode($list['imgs']);
                }
            }
            $this->assign('zt', $zt);
            $this->assign('lists', $lists);
            $this->assign('qid', $qid);
            return view();
        } else {
            if ($request->param()['msg']) {
                $gonngdan = db('gongdan')->where('id', $qid)->find();
                $data['uid'] = $gonngdan['uid'];
                $data['uname'] = $gonngdan['uname'];
                $data['title'] = '';
                $data['msg'] = $request->param()['msg'];
                $data['imgs'] = '';
                $data['time'] = time();
                $data['type'] = 's';
                $data['qid'] = $qid;
                $data['zt'] = 0;
                $ret = db('gongdan')->insertGetId($data);
                //回复并结单
                if ($request->param()['gd_zt']) {
                    $ret_zt = db('gongdan')->where('id', $qid)->update(['zt' => 1]);
                }
                if ($ret) {
                    $this->success('回复成功');
                } else {
                    $this->error('回复失败');
                }
            } else {
                $this->error('不能提交空消息');
            }
        }
    }

    public function shiming_list()
    { //实名认证列表
        $request = Request::instance();
        $param = $request->param();
        $where = [];
        if (isset($param['zt'])) {
            if ($param['zt'] == '1') {
                $where['zt'] = 1;
            } elseif ($param['zt'] == '2') {
                $where['zt'] = 2;
            } elseif ($param['zt'] == '3') {
                $where['zt'] = 3;
            }
        }else{
            $param['zt'] = '3';
        }
        if(!empty($param['uname'])){
            $where['uname'] = $param['uname'];
        }
        $lists = db('shiming')->where($where)->order('time DESC')->paginate(10,false,['query'=>$request::instance()->param()]);
        $this->assign('lists', $lists);
        $this->assign('param', $param);
        $this->assign('oss_prefix', config('OSS_PREFIX'));
        return view();
    }

    /**
     * @name 实名认证 （通过之后送云矿）
     * @author zay
     * @Date 2020/3/30 0030 16:07
     */

    public function shiming_zt()
    {   //修改实名状态
        $request = Request::instance();
        $id = $request->param()['id'];
        $zt = $request->param()['zt'];

        if (!in_array($zt, [1, 2])) {
            $this->error('非法操作');
        }
        $shiming = db('shiming')->where('id', $id)->find();
        if (!$shiming) {
            $this->error('信息不存在');
        }
        Db::startTrans();   // 启动事务-------------
        $ret4 = true;
        if ($zt == 1) {
            $ret1 = db('shiming')->where('id', $id)->update(['zt' => $zt, 'note' => '']);
            //用户信息
            $userinfo = userinfobyid($shiming['uid']);
            //矿机信息
            $kj_info = DB('shop_project')->where(['id' => 7])->find();
            //如果已经有了云矿就不送
            $yun_kj = Db('shop_orderform')->where(['uid' => $userinfo['id'], 'type' => 2, 'kj_type' => 7])->find();
            if (empty($yun_kj)) {
                //实名认证之后送云矿
                $insert_data = [
                    'mobile' => $userinfo['uname'],
                    'project' => $kj_info['name'],
                    'addtime' => time(),
                    'zt' => 0,
                    'imagepath' => $kj_info['imagepath'],
                    'yxzq' => $kj_info['yxzq'],
                    'price' => $kj_info['price'],
                    'kjsl' => $kj_info['kjsl'],
                    'kjbh' => orderSn(),
                    'uid' => $userinfo['id'],
                    'capacity' => $kj_info['capacity'],
                    'type' => 2,
                    'kj_type' => 7
                ];
                $ret4 = Db('shop_orderform')->insert($insert_data);
            }
        } else {
            $ret1 = db('shiming')->where('id', $id)->update(['zt' => $zt]);
        }

        $ret2 = db('user')->where('id', $shiming['uid'])->update(['shiming' => $zt, 'sex' => $shiming['sex'], 'truename' => $shiming['truename'],'update_time'=>time()]);

        if ($ret1 && $ret2 && $ret4) {
            Db::commit();       // 提交事务-------------
            $this->success('操作成功');
        } else {
            Db::rollback();     // 回滚事务-------------
            $this->error('操作失败');
        }
    }

    public function shiming_note()
    { //ajax修改实名备注
        $id = request()->param()['id'];
        $note = request()->param()['note'];
        $ret = db('shiming')->where('id', $id)->update(['note' => $note]);
        if ($ret) {
            return json(['zt' => 1, 'msg' => '修改成功']);
        } else {
            return json(['zt' => 0, 'msg' => '修改失败']);
        }
    }

    public function shequ_list()
    { //社区申请列表
        $param = request()->param();
        $where = [];
        if (isset($param['zt'])) {
            if ($param['zt'] == '1') {
                $where['zt'] = 1;
            } elseif ($param['zt'] == '2') {
                $where['zt'] = 2;
            }
        } else {
            $where['zt'] = 0;
        }
        $lists = db('shequ')->where($where)->order('ctime DESC')->paginate(10);
        $this->assign('lists', $lists);
        return view();
    }

    public function shequ_zt()
    {   //修改社区状态
        $request = Request::instance();
        $id = $request->param()['id'];
        $zt = $request->param()['zt'];

        if (!in_array($zt, [1, 2])) {
            $this->error('非法操作');
        }
        $shequ = db('shequ')->where('id', $id)->find();
        if (!$shequ) {
            $this->error('信息不存在');
        }
        Db::startTrans();   // 启动事务-------------

        if ($zt == 1) {
            $ret1 = db('shequ')->where('id', $id)->update(['zt' => $zt, 'ctime' => time(), 'xuanyan' => '社区开通了', 'note' => '']);
            $ret2 = db('user')->where('id', $shequ['uid'])->update(['issq' => $shequ['id']]);
        } else {
            $ret1 = db('shequ')->where('id', $id)->update(['zt' => $zt, 'ctime' => time()]);
            $ret2 = db('user')->where('id', $shequ['uid'])->update(['issq' => 0]);
        }
        if ($ret1) {
            Db::commit();       // 提交事务-------------
            $this->success('审核成功');
        } else {
            Db::rollback();     // 回滚事务-------------
            $this->error('审核失败');
        }
    }

    public function shequ_note()
    { //ajax修改社区备注
        $id = request()->param()['id'];
        $note = request()->param()['note'];
        $ret = db('shequ')->where('id', $id)->update(['note' => $note]);
        if ($ret) {
            return json(['zt' => 1, 'msg' => '修改成功']);
        } else {
            return json(['zt' => 0, 'msg' => '修改失败']);
        }
    }

    public function tiaozhan()
    {
        $where = [];
        $lists = db('renwu_user')->where($where)->order(['btime' => 'desc'])->paginate(10);
        $this->assign('lists', $lists);
        return view();
    }

    /**
     * @name 解封和封账号
     * @author zay
     * @Date 2020/4/2 0002 14:01
     * @return \think\response\Json
     */
    public function update_zt()
    {
        $is_error = false;
        $message = '';
        $uid = request()->param()['id'];
        $value = request()->param()['value'];
        $reason = request()->param()['reason'];
        $userinfo = userinfobyid($uid);
        if (!$userinfo) {
            $is_error = true;
            $message = '<p style="color:red;">没有此用户</p>';
            $data = array(
                'zt' => $is_error,
                'message' => $message
            );
            return json($data);
        }
        if ($value != 1 and $value != 2) {
            $is_error = true;
            $message = '<p style="color:red;">非法操作</p>';
            $data = array(
                'zt' => $is_error,
                'msg' => $message
            );
            return json($data);
        }
        //修改状态开始
        if (!$is_error) {
            $is_ok = db('user')->where('id', $uid)->update(['zt' => $value,'reason'=>$reason]);
            $message = '操作成功';
        }


        //修改状态结束
        $data = array(
            'zt' => $is_error,
            'msg' => $message
        );
        return json($data);
    }

    /**
     * @name 用户实名信息
     * @author zay
     * @Date 2020/4/2 0002 14:38
     * @return array
     */

    public function shiming(Request $request)
    {
        $uid = $request->param('uid', 0);
        //获取用户信息
        $userinfo = userinfobyid($uid);
        if (!$userinfo) {
            $this->error('用户不存在');
        }
        $shiming = Db('shiming')->where(['uid' => $uid])->find();
        $this->assign('list', $shiming);
        $this->assign('userinfo', $shiming);
        return view();
    }

    /**
     * @name 提交实名信息
     * @author zay
     * @Date 2020/4/2 0002 15:20
     * @param Request $request
     * @return \think\response\View
     */

    public function shiming_do(Request $request)
    {
        $uid = $request->param('uid', 0);
        //获取用户信息
        $userinfo = userinfobyid($uid);
        if (!$userinfo) {
            $this->error('用户不存在');
        }
        if (!$request->isPost()) {
            $shiming = Db('shiming')->where(['uid' => $uid])->find();
            $this->assign('list', $shiming);
            return view();
        }
        Db::startTrans();
        try {
            $uid = $request->param('uid', 0);
            //封面图上传
            $sfz1 = request()->file('sfz1');
            $sfz2 = request()->file('sfz2');
            $shouchi = request()->file('shouchi');
            $data = [
                'sfz1' => $sfz1,
                'sfz2' => $sfz2,
                'shouchi' => $shouchi,
            ];
            $image_url = [];
            foreach ($data as $k => $file) {
                if ($file) {
                    $file = str_replace("\\", "/", $file->validate(['size' => 2048000, 'ext' => 'jpg,png,gif,bmp,jpeg,ico'])
                        ->move(ROOT_PATH . 'public' . DS . 'image' . DS . 'shiming', $userinfo['uname'] . '_' . $k)
                        ->getSaveName());
                    $image = \think\Image::open('image/shiming/' . $file);
                    $image->thumb(150, 150, \think\Image::THUMB_CENTER)->save('image/shiming/' . $file);
                    // $file = '/image/shiming/' . $file;
                    $image_url[$k] = 'image/shiming/' . $file;
                }
            }
            //检查用户是否已经提交过信息
            $shiming = Db('shiming')->where(['uid' => $userinfo['id']])->find();
            $update_data = [
                'uid' => $userinfo['id'],
                'uname' => $userinfo['uname'],
                'sex' => $request->param('sex', 0),
                'truename' => $request->param('truename', ''),
                'sfz' => $request->param('sfz', ''),
                'sfz1' => isset($image_url['sfz1']) ? $image_url['sfz1'] : '',
                'sfz2' => isset($image_url['sfz2']) ? $image_url['sfz2'] : '',
                'shouchi' => isset($image_url['shouchi']) ? $image_url['shouchi'] : '',
                'address' => $request->param('address', ''),
                'time' => time(),
            ];
            if ($shiming) {
                $res = Db('shiming')->where(['uid' => $userinfo['id']])->update($update_data);
            } else {
                $update_data['zt'] = 1;
                $res = Db('shiming')->insert($update_data);
                //实名之后送矿机
                //矿机信息
                $kj_info = DB('shop_project')->where(['id' => 7])->find();
                //如果已经有了云矿就不送
                $yun_kj = Db('shop_orderform')->where(['uid' => $userinfo['id'], 'type' => 2, 'kj_type' => 7])->find();
                if (empty($yun_kj)) {
                    //实名认证之后送云矿
                    $insert_data = [
                        'mobile' => $userinfo['uname'],
                        'project' => $kj_info['name'],
                        'addtime' => time(),
                        'zt' => 0,
                        'imagepath' => $kj_info['imagepath'],
                        'yxzq' => $kj_info['yxzq'],
                        'price' => $kj_info['price'],
                        'kjsl' => $kj_info['kjsl'],
                        'kjbh' => orderSn(),
                        'uid' => $userinfo['id'],
                        'capacity' => $kj_info['capacity'],
                        'type' => 2,
                        'kj_type' => 7
                    ];
                    $ret4 = Db('shop_orderform')->insert($insert_data);
                    if (!$ret4) {
                        throw new \Exception('发放矿机失败');
                    }
                }
            }
            if (!$res) {
                throw new \Exception('实名信息保存失败');
            }
            //修改用户信息表
            $res = Db('user')->where(['id' => $userinfo['id']])
                ->update([
                    'shiming' => 1,
                    'update_time' => time(),
                    'truename' => $request->param('truename', '')
                ]);
            if (!$res) {
                throw new \Exception('保存失败');
            }
            Db::commit();
            $this->success('提交成功');
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
    }

    // 赠送列表
    public function give()
    {
        $project = db('shop_project')->field(true)->select();
        $project_mobile = array();
        $logocoin_uname = array();

        if (input('project_mobile')) $project_mobile['mobile'] = input('project_mobile');

        if (input('logocoin_uname')) $logocoin_uname['uname'] = input('logocoin_uname');


        $give_project = db('shop_orderform')->where($project_mobile)->where('type', 4)->order('id desc')->paginate(20);
        $give_logocoin = db('logocoin')->where($logocoin_uname)->where('type', 11)->order('id desc')->paginate(20);

        $this->assign('project', $project);
        $this->assign('give_project', $give_project);
        $this->assign('give_logocoin', $give_logocoin);
        return view();
    }

    // 赠送矿机
    public function give_kj()
    {
        $this->error('此功能暂停使用');
        $uname = input('uname');
        $project_id = input('project');

        if (!$uname) $this->error('请输入用户名');
        if (!$project_id) $this->error('请选择赠送的矿机');

        $orderSn = orderSn(); //订单生成
        $userinfo = db('user')->where('uname', $uname)->find();
        if (!$userinfo) $this->error('请输入用户名不存在');
        $project = db('shop_project')->where('id', $project_id)->find();

        $map['mobile'] = $userinfo['uname']; // 用户手机号
        $map['uid'] = $userinfo['id']; // 用户ID
        $map['project'] = $project['name']; // 矿加名称
        $map['yxzq'] = $project['yxzq']; //运行周期（天）
        $map['price'] = $project['price'];  // 矿加价格
        $map['addtime'] = time();
        $map['imagepath'] = $project['imagepath']; // 图片
        $map['capacity'] = $project['capacity']; // 产量总币
        $map['type'] = 4;
        $map['kjsl'] = $project['kjsl'];// 算力
        $map['kjbh'] = $orderSn; // 订单
        $map['kj_type'] = $project_id;

        db('shop_orderform')->insert($map);

        // 提交事务
        $this->success('赠送成功');

    }

    // 赠送币
    public function give_b()
    {

        $this->error('此功能暂停使用');
        $uname = input('uname');
        $idc = input('idc');

        if (!$uname) $this->error('请输入用户名');
        if (!$idc) $this->error('请输入你要赠送的数量');
        $userinfo = db('user')->where('uname', $uname)->find();
        if (!$userinfo) $this->error('请输入用户名不存在');
        db('user')->where(array('uname' => $uname))->setInc('idc', $idc);
        $logocoin["uname"] = $userinfo['uname']; // 用户名
        $logocoin["uid"] = $userinfo['id']; // 金幣
        $logocoin["type"] = 11;
        $logocoin["mqian"] = $userinfo['idc'];//当前帐户金币余额
        $logocoin["money"] = $idc;//变动数量
        $logocoin["mhou"] = $userinfo['idc'] + $idc;//变动之后的
        $logocoin["note"] = '赠送IDC币';
        $logocoin["time"] = time();
        db('logocoin')->insert($logocoin);
        $this->success('赠送成功');

    }


    public function getnextree()
    {    //KM++ajax获取树形图下级

        $param = request()->param();
        $users = db('user');
        $userid = isset($param['id']) ? $param['id'] : 1;
        if (input('uname')) {
            $rs = $users->where('uname', $param['uname'])->find();
        } else {
            $rs = $users->where(array('id' => $userid))->find();
        }


        $tjtreestr = '';

        if ($rs) {
            $list = $users->where(array('tname' => $rs['uname']))->select();
            foreach ($list as $tkey => $tval) {
                $tjtreestr .= '<div>';
                $xjcount = $users->where(array('uname' => $tval['tname']))->field('uname')->count();

                if (0 < $xjcount) {
                    $tjtreestr .= '<a href=\'#tree_' . $tval['id'] . '\' onclick="findtree(' . $tval['id'] . ',\'' . url('getnextree') . '\')">';

                    if ($tkey == count($list) - 1) {
                        $tjtreestr .= '<img id=\'tree_' . $tval['id'] . '\' src=\'/public/admin/wangluotu/tree_plusl.gif\' border=0>';
                    } else {
                        $tjtreestr .= '<img id=\'tree_' . $tval['id'] . '\' src=\'/public/admin/wangluotu/tree_plus.gif\' border=0>';
                    }
                } else if ($tkey == count($list) - 1) {
                    $tjtreestr .= '<img id=\'tree_' . $tval['id'] . '\' src=\'/public/admin/wangluotu/tree_blankl.gif\' border=0>';
                } else {
                    $tjtreestr .= '<img id=\'tree_' . $tval['id'] . '\' src=\'/public/admin/wangluotu/tree_blank.gif\' border=0>';
                }
                $tjtreestr .= '</a> ' . $tval['uname']/* . '  - 人数：' .$zrs. '  - 算力：' .$zkjsl */
                ;
                $tjtreestr .= '<div id="nextree_' . $tval['id'] . '" style="margin-left:30px;display:none"></div>';
                $tjtreestr .= '</div>';
            }
            $data['result'] = 1;
            $data['html'] = $tjtreestr;
        } else {
            $data['result'] = 0;
            $data['html'] = '';
        }
        if (request()->post()) {
            return $data;
        }
        $this->assign('data', $data['html']);
        return view();
    }
}
